package GUIs;

import BaseDeDatos.ManejadorBD;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tinacho
 */
public class GUI_ListadoCorredores extends javax.swing.JFrame {

    private Connection conexion = null;

    public GUI_ListadoCorredores() {
        initComponents();
        centrarJFrame();
    }

    // ---------------------------------------------------------
    // ------- Seccion de metodos publicos : Getters
    // ---------------------------------------------------------
    
    /**
     * 
     * @return el valor corriente de la variable 'jbutton_Cerrar'
     */    
    public JButton obtenerJB_Cerrar(){
        return this.jbutton_Cerrar;
    }// Fin obtenerJB_Cerrar
   
    
    /**
     * @brief Carga la tabla con la informacion de la base de datos.
     * 
     * @param c Repreenta la conexion con la base de datos.
     * @throws SQLException 
     */    
    public void llenarTablaConCorredores(Connection c) throws SQLException{
        this.conexion = c;
        ManejadorBD manejadorC = new ManejadorBD(this.conexion);
        ResultSet listaCorredores = manejadorC.buscarCorredores();
        while(listaCorredores.next()){
           ((DefaultTableModel)this.jTable_Corredores.getModel()).addRow(new Object[]{
                listaCorredores.getString("dni"),
                listaCorredores.getString("nombre"),
                listaCorredores.getString("apellido"),
                listaCorredores.getString("categoria"),
                listaCorredores.getString("direccion"),
                listaCorredores.getString("telefono"),
                listaCorredores.getString("edad")
            });            
        }
    }// Fin llenarTablaConCorredores
    

    /**
     * Metodo encargado de asociar los eventos correspondientes a 
     * los diferentes componentes de la interfaz
     * 
     * @param lis
     */
    public void setActionListeners(ActionListener lis) {
        this.jbutton_Cerrar.addActionListener(lis);   
    }// Fin setActionListeners    
    
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_Corredores = new javax.swing.JTable();
        label_titulo = new javax.swing.JLabel();
        jbutton_Cerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Listado de corredores");
        setAlwaysOnTop(true);
        setResizable(false);

        jTable_Corredores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DNI", "Nombre", "Apellido", "Categoría", "Edad", "Dirección", "Teléfono"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable_Corredores);

        label_titulo.setFont(new java.awt.Font("Andalus", 0, 24)); // NOI18N
        label_titulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label_titulo.setText("Listado de corredores cargados en la base de datos del sistema");

        jbutton_Cerrar.setText("Cerrar listado");
        jbutton_Cerrar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(label_titulo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(424, Short.MAX_VALUE)
                .addComponent(jbutton_Cerrar)
                .addGap(408, 408, 408))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(label_titulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbutton_Cerrar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void centrarJFrame(){
        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frame =this.getSize();
        this.setLocation((pantalla.width-frame.width)/2, (pantalla.height-frame.height)/2);
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI_ListadoCorredores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI_ListadoCorredores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI_ListadoCorredores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI_ListadoCorredores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI_ListadoCorredores().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_Corredores;
    private javax.swing.JButton jbutton_Cerrar;
    private javax.swing.JLabel label_titulo;
    // End of variables declaration//GEN-END:variables
}
